package br.com.cubelogic.sql;

import java.sql.*;
import java.util.*;
import br.com.cubelogic.sql.data.*;

public abstract class AbstractConnectionHandler {

	protected Properties dbProperties, queries;
	protected Connection conn;
	protected DBEngine engine;
	
	private AbstractConnectionHandler(DBEngine engine, Properties dbProperties, Properties queries){
		this.engine = engine;
		this.dbProperties = dbProperties;
		this.queries = queries;
	}
	
	protected abstract void loadDbProperties(String file);
	
	protected abstract void loadQueries(String file);
	
	protected final void connect() throws ClassNotFoundException, SQLException{
		Class.forName(engine.getDriver());
		if(engine == DBEngine.jTDS){
			this.conn = DriverManager.getConnection(engine.getInit() + dbProperties.getProperty("server") + ":" + dbProperties.getProperty("port") + "/" + dbProperties.getProperty("dbName") + ";user=" + dbProperties.getProperty("user") + ";password=" + dbProperties.getProperty("pass"));
		}else{
			if(engine == DBEngine.MySQL){
				this.conn = DriverManager.getConnection(engine.getInit() + dbProperties.getProperty("server") + ":" + dbProperties.getProperty("port") + "/" + dbProperties.getProperty("dbName") + "?user=" + dbProperties.getProperty("user") + "&password=" + dbProperties.getProperty("pass"));
			}
		}
	}
	
	protected final Connection getConnection(){
		return this.conn;
	}
}